
package com.meizi.codegenerate.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.util.Assert;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 转换工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
public class ConvertUtils {
	private static Logger logger = LoggerFactory.getLogger(ConvertUtils.class);

	public static <T> T sourceToTarget(Object source, Class<T> target) {
		if (source == null) {
			return null;
		}
		T targetObject = null;
		try {
			targetObject = target.newInstance();
			BeanUtils.copyProperties(source, targetObject);
		} catch (Exception e) {
			logger.error("convert error ", e);
		}

		return targetObject;
	}

	public static <T> List<T> sourceToTarget(Collection<?> sourceList, Class<T> target) {
		if (sourceList == null) {
			return null;
		}

		List targetList = new ArrayList<>(sourceList.size());
		try {
			for (Object source : sourceList) {
				T targetObject = target.newInstance();
				BeanUtils.copyProperties(source, targetObject);
				targetList.add(targetObject);
			}
		} catch (Exception e) {
			logger.error("convert error ", e);
		}

		return targetList;
	}

	public static void fillDateProperty(Collection sources, String dateLongProperty, String dateProperty)
			throws BeansException {
		for (Object source : sources) {
			fillDateProperty(source, dateLongProperty, dateProperty);
		}
	}

	/**
	 * 从Long 字段，填充对应的Date字段
	 * 
	 * @param source
	 * @param dateLongProperty
	 * @param dateProperty
	 * @throws BeansException
	 */
	public static void fillDateProperty(Object source, String dateLongProperty, String dateProperty)
			throws BeansException {
		Assert.notNull(source, "Source must not be null");
		Class<?> actualEditable = source.getClass();
		PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
		// PropertyDescriptor[] var7 = targetPds;
		int length = targetPds.length;
		Method writeMethod = null;
		Method readMethod = null;
		for (int i = 0; i < length; ++i) {
			PropertyDescriptor targetPd = targetPds[i];
			if (targetPd.getName().equalsIgnoreCase(dateLongProperty)) {
				readMethod = targetPd.getReadMethod();
			}
			if (targetPd.getName().equalsIgnoreCase(dateProperty)) {
				writeMethod = targetPd.getWriteMethod();
			}
		}
		if ((writeMethod != null) && (writeMethod.getParameterCount() == 1)
				&& (writeMethod.getParameterTypes()[0] == Date.class) && (readMethod.getReturnType() == Long.class)
				&& (readMethod != null)) {
			try {
				if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
					readMethod.setAccessible(true);
				}
				Long value = (Long) readMethod.invoke(source);
				if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
					writeMethod.setAccessible(true);
				}
				writeMethod.invoke(source, new Date(value));
			} catch (Throwable var15) {
				// throw new FatalBeanException("Could not copy property '" + targetPd.getName()
				// + "' from source to target", var15);
			}
		}
	}

	/*
	 * public static void main(String[] args) { Test t = new Test(); t.setDl(1L);
	 * ConvertUtils.fillDateProperty(t, "dl", "date");
	 * System.out.println(t.getDate()); }
	 */
}

/*
 * @Data class Test { private Long dl; private Date date; }
 */
